<?php

include_once("dbconnect.php");

function printTable() {
    $str = "<div class='row'>
                <div class='col-lg-12'>
                    <div class='panel panel-default'>
                        <div class='panel-heading'>
                            Danh sách khách hàng
                        </div>
                        <!-- /.panel-heading -->
                        <div class='panel-body'>
                            <div class='table-responsive'>
                                <table class='table table-striped table-bordered table-hover' id='dataTables-example'>
                                    <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>Họ tên</th>
                                            <th>Địa chỉ</th>
                                            <th>Số điện thoại</th>
                                            <th>Thư điện tử</th>
                                            <th>Chỉnh sửa</th>
                                        </tr>
                                    </thead>
                                    <tbody>";
    $result = mysqli_query(getConnect(), "Select * from customers");
    while ($row = mysqli_fetch_array($result)) {
        $str = $str . "     <tr class='odd gradeX'>
                                            <td>${row['id']}</td>
                                            <td>${row['name']}</td>
                                            <td>${row['address']}</td>
                                            <td>${row['phone']}</td>
                                            <td>${row['email']}</td>  										
                                            <td align ='center'><i class='fa fa-pencil-square-o fa-3x' data-toggle='modal' data-target='#myModal' value='edit'></i></td>
                                        </tr>";
    }
    $str = $str . " </tbody>
                                </table>
                            </div> ";
    echo $str;
}

if (isset($_POST['edit'])) {
    if ($_POST['edit'] == "Thêm mới") {
        insert();
    } elseif ($_POST['edit'] == "Cập nhật") {
        update();
    } elseif ($_POST['edit'] == "Xóa") {
        delete();
    }
}

function insert() {
    $email = $_POST['email'];
    $name = $_POST['name'];
    $add = $_POST['add'];
    $id = $_POST['id'];
    $phone = $_POST['phone'];
    $query = "INSERT INTO `customers`(`name`, `address`, `phone`, `email`) VALUES('$name','$add','$phone','$email')";
    echo $query;

    $result = mysqli_query(getConnect(), $query);

    if ($result) {
        echo "Success!";
		$location = "/qllk/customers.php";
		header("Location: "."http://". $_SERVER['HTTP_HOST']. $location	);
        
        closeConnect();
    } else {
        die('not update ' . mysqli_error($result));
    }
}

function update() {
//     echo $_POST['name'];
    $email = $_POST['email'];
    $name = $_POST['name'];
    $add = $_POST['add'];
    $id = $_POST['id'];
    $phone = $_POST['phone'];
    //echo $id,$phone,$name,$add,$email;
    $query = "UPDATE customers SET name ='$name', email = '$email' , address='$add', phone='$phone' WHERE id='$id'";
    echo $query;

    $result = mysqli_query(getConnect(), $query);

    if ($result) {
        echo "Update Success!";
       $location = "/qllk/customers.php";
		header("Location: ". "http://" . $_SERVER['HTTP_HOST'] . $location);
        closeConnect();
    } else {
        die('Update failed ' . mysqli_error($result));
    }
}

function delete() {
    $id = $_POST['id'];
    $query = "DELETE FROM customers  WHERE id='$id'";
    $result = mysqli_query(getConnect(), $query);

    if ($result) {
        echo "Delete Success!";
        $location = "/qllk/customers.php";
		header("Location: "."http://". $_SERVER['HTTP_HOST']. $location);
        closeConnect();
    } else {
        die('Delete failed ' . mysqli_error($result));
    }
    echo "delete";
}
?>

